package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.Customer;
import com.rockcent.mall.lib.domain.CustomerBank;
import com.rockcent.mall.lib.dto.ArticleDto;
import com.rockcent.mall.lib.dto.CustomerBankDto;
import com.rockcent.mall.lib.repository.CustomerBankRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2016/9/23.
 */
@Repository
public class CustomerBankRepositoryImpl implements CustomerBankRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(CustomerBankRepositoryImpl.class);

    @Autowired
    private  NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public CustomerBankDto showCustomerBankInfor(Long customerBankId) {

        SQLObjectDoing<CustomerBankDto> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT b.`id`, b.bank_card as accountNo,b.card_holder as bankCardHolder,a.name as bankTypeName,c.phone as customerPhone FROM bank a,customer_bank b, customer c" +
                    "  WHERE c.`id`=b.`customer_id` AND a.`id`=b.`bank_type_id` ");
            sql.append(" AND b.`id` = :customerBankId");

            params.put("customerBankId", customerBankId);

            log.debug(sql.toString());
        };
        return result.go(CustomerBankDto.class, jdbcTemplate, log,objectMapper);
    }

    @Override
    public boolean updateCustomerBankInfor(CustomerBankDto vo) {
//        CustomerBank card=customerBankCardDao.findOne(vo.getId());
//       Long customerId= card.getCustomerId();
//        Customer customer = customerDao.findOne(customerId);
//        customer.setMobile(vo.getCustomerPhone());
//
//        if(card!=null){
//            card.setCardHolder(vo.getBankCardHolder());
//            card.setBankCard(vo.getAccountNo());
//            if(bankDao.exists(vo.getBankTypeId())) {
//                card.setBankTypeId(vo.getBankTypeId());
//            }
//        }
//        customerBankCardDao.save(card);
//        customerDao.save(customer);
        return true;
    }
}
